package com.example.demo.service.imp;

import com.example.demo.entity.User;
import com.example.demo.entity.rep.UserRep;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.utils.BusinessException;
import com.example.demo.utils.JwtUtils;
import com.example.demo.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.lang.Collections;
import jodd.net.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;

     private static final int TOKEN_EXPIRE_TIME  = 1000 * 60 * 60 * 2; // 60 * 60 * 2
     private static final int REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24; // 24h ms

    @Override
    public Page<User> getAll(String username, int pageNum, int pageSize) {
        List<User> users = userMapper.selectByName(username, pageSize, pageNum);

        PageInfo<User> pageInfo = new PageInfo<>(users);
        Page page = new Page<User>();
        return page;
    }

    @Override
    public Boolean register(User user) {
        User user1 = userMapper.findByUsername(user.getUsername());
        if(user1 != null){
            throw new BusinessException(3001,"用户已存在");
        }
       return userMapper.insert(user)>0;
    }

    @Override
    public UserRep login(String account, String password) {
//         进行登录，登录后续生成token
        User user = userMapper.findByAccount(account);
        if(user ==null){
            throw new BusinessException(1001,"用户不存在");
        }
        if(!user.getPassword().equals(password)){
            throw new BusinessException(1002,"密码错误");
        }
        UserRep userRep = new UserRep();
        String token = JwtUtils.generateToken(TOKEN_EXPIRE_TIME,account);//
        String refreshToken = JwtUtils.generateToken(REFRESH_TOKEN_EXPIRE_TIME,account);// 1天
//        生成token
        userRep.setToken(token);
        userRep.setRefreshToken(refreshToken);
        userRep.setUuid(user.getUuid());
        userRep.setUsername(user.getUsername());
        userRep.setAccount(user.getAccount());
        userRep.setCreatedAt(user.getCreatedAt());
        userRep.setUpdatedAt(user.getUpdatedAt());
        userRep.setCreatedBy(user.getCreatedBy());
        userRep.setUpdatedBy(user.getUpdatedBy());
        return userRep;
    }

    @Override
    public Boolean updateUser(User user) {
        return null;
    }

    @Override
    public UserRep refreshToken(String token)  {
            UserRep userRep = new UserRep();
//        校验 refreshToken
            JwtUtils.parseToken(token);
            String account = JwtUtils.getUsername(token);
            userRep.setToken(JwtUtils.generateToken(TOKEN_EXPIRE_TIME,account));
            return userRep;
    }

    @Override
    public Boolean deleteUser(int id) {
        return null;
    }

    @Override
    public User getUserById(int id) {
        User user = userMapper.findById(id);
        return user;
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }
}
